package com.base.cn.platform.os.controller.count;

import com.base.cn.platform.os.common.AdminBaseController;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.service.manage.order.count.OrderCountService;
import com.base.cn.platform.os.service.manage.user.count.UserCountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 营销数据统计Controller
 *
 * @author s.li
 * @create 2018-08-24-15:52
 */
@Controller
@RequestMapping("/admin/count")
public class SalesStatisticsController extends AdminBaseController {

    @Autowired
    private UserCountService userCountService;
    @Autowired
    private OrderCountService orderCountService;

    /**
     * 初化营销数据统计页面
     * @return
     */
    @RequestMapping("/salesCount")
    public String salesCount(){
        return resultPage("/count/sales-statistics");
    }

    /**
     * 查询营销数据统计的基本数据统计
     * @return Map<String,Object>
     */
    @RequestMapping("/salesBaseCount")
    @ResponseBody
    public Map<String,Object> salesBaseCount(){
        //用户数量
        Map<String,Integer>  userCountMap = userCountService.findUserCount();
        //订单总数
        int orderCountNum = orderCountService.queryOrderCountNum();
        //流水总额
        double orderSumPrice = orderCountService.queryOrderSumPrice();
        Map<String,Object> dataMap = new HashMap<>();
        dataMap.put("userCountMap",userCountMap);
        dataMap.put("orderCountNum",orderCountNum);
        dataMap.put("orderSumPrice",orderSumPrice);
        return ResultUtil.SUCCESS(dataMap);
    }

    /**
     * 不同专业项目销转对比数据
     * @return Map<String,Object>
     */
    @RequestMapping("/queryOrderSubjectPriceCountNum")
    @ResponseBody
    public Map<String,Object> queryOrderSubjectPriceCountNum(){
        List<Map<String,Object>> orderSubjectPriceNumList = orderCountService.queryOrderSubjectPriceCountNum();
        return ResultUtil.SUCCESS(orderSubjectPriceNumList);
    }

    /**
     * 查询统计订单价格区域的数据
     * @return Map<String,Object>
     */
    @RequestMapping("/queryOrderPriceCountNum")
    @ResponseBody
    public Map<String,Object> queryOrderPriceCountNum(){
        Map<String,Integer> orderPriceCountNumMap = orderCountService.queryOrderPriceCountNum();
        int max = 0;
        if(ObjectUtils.isNotEmpty(orderPriceCountNumMap)){
            List<String> keyList = new ArrayList<>(orderPriceCountNumMap.keySet());
            for(String key : keyList){
                max = Math.max(max , orderPriceCountNumMap.get(key));
            }
        }
        Map<String,Object> resultMap = ResultUtil.SUCCESS(orderPriceCountNumMap);
        resultMap.put("max",max);
        return resultMap;
    }

    /**
     * 查询订单支付方式占比统计数据
     * @return Map<String,Object>
     */
    @RequestMapping("/queryOrderPayTypeCountNum")
    @ResponseBody
    public Map<String,Object> queryOrderPayTypeCountNum(){
        Map<String,Integer> orderPayTypeCountNumMap = orderCountService.queryOrderPayTypeCountNum();
        Map<String,Object> resultMap = ResultUtil.SUCCESS(orderPayTypeCountNumMap);
        return resultMap;
    }

    /**
     * 查询订单的来源数据统计
     * @return Map<String,Object>
     */
    @RequestMapping("/queryOrderFormCountNum")
    @ResponseBody
    public Map<String,Object> queryOrderFormCountNum(){
        Map<String,Integer> orderFormCountNum = orderCountService.queryOrderFormCountNum();
        return ResultUtil.SUCCESS(orderFormCountNum);
    }

}
